package com.wlsk.license.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wlsk.license.dao.entity.ChannelOrder;
import com.wlsk.license.dao.mapper.ChannelOrderMapper;
import com.wlsk.license.dto.ChannelOrderDetail;
import com.wlsk.license.dto.request.ChannelOrderReqDto;
import com.wlsk.license.service.IChannelOrderService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 渠道订单信息表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2025-10-17
 */
@Service
public class ChannelOrderServiceImpl extends ServiceImpl<ChannelOrderMapper, ChannelOrder> implements IChannelOrderService {


    @Override
    public ChannelOrderDetail detail(ChannelOrderReqDto reqDto) {
        ChannelOrderDetail detail = new ChannelOrderDetail();
        try {
            ChannelOrder order = this.getBaseMapper().selectOne(Wrappers.<ChannelOrder>lambdaQuery().eq(ChannelOrder::getChannelBusinessId, reqDto.getChannelBusinessId()).eq(ChannelOrder::getChannelNumber, reqDto.getChannelNumber()));
            if(order != null) {
                detail = JSONObject.parseObject(order.getOrderJson(), ChannelOrderDetail.class);
            } else {
                // TODO 查询运调系统数据
                ChannelOrder channelOrder = new ChannelOrder();
                channelOrder.setChannelBusinessId(reqDto.getChannelBusinessId());
                channelOrder.setChannelNumber(reqDto.getChannelNumber());
                this.save(channelOrder);
            }
        } catch (Exception e) {
            log.error("查询渠道订单信息异常", e);
        }
        return detail;
    }
}
